package com.xoa.service.financialManage.charge.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.xoa.dao.financialManage.charge.ChargeTypeMapper;
import com.xoa.model.financialManage.charge.ChargeType;
import com.xoa.model.financialManage.charge.ChargeTypeExample;
import com.xoa.service.financialManage.charge.ChargeTypeService;
import com.xoa.util.ToJson;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 费用类型 服务实现类
 * </p>
 *
 * @author liujian
 * @since 2020-04-23
 */
@Service
public class ChargeTypeServiceImpl extends ServiceImpl<ChargeTypeMapper, ChargeType> implements ChargeTypeService {

    @Resource
    ChargeTypeMapper chargeTypeMapper;


    @Override
    public ToJson<ChargeType> save(ChargeType chargeType) {
        ToJson<ChargeType> toJson = new ToJson<>(1,"err");
        try{
            chargeType.setCreatedTime(new Date());
            int i = chargeTypeMapper.insertSelective(chargeType);
            if (i > 0) {
                List<ChargeType> childList = chargeType.getChildList();
                if (childList != null) {
                    childList.stream().forEach(type -> {
                        type.setCreatedTime(new Date());
                        type.setChargeTypeParent(chargeType.getChargeTypeId());
                        chargeTypeMapper.insertSelective(type);
                    });
                }

                toJson.setFlag(0);
                toJson.setMsg("true");
            }
        }catch (Exception e){
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }

        return toJson;
    }

    @Override
    public ToJson<ChargeType> deleteById(Integer chargeTypeId) {
        ToJson<ChargeType> toJson = new ToJson<>(1,"err");
        try{
            //删除类型及子类
            int i = chargeTypeMapper.deleteByPrimaryKey(chargeTypeId);
            chargeTypeMapper.deleteByParent(chargeTypeId);
            if(i>0){
                toJson.setFlag(0);
                toJson.setMsg("true");
            }
        }catch (Exception e){
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }

        return toJson;
    }

    @Override
    public ToJson<ChargeType> update(ChargeType chargeType) {
        ToJson<ChargeType> toJson = new ToJson<>(1,"err");
        try{
            int i = chargeTypeMapper.updateByPrimaryKeySelective(chargeType);
            if(i>0){
                toJson.setFlag(0);
                toJson.setMsg("true");
            }
        }catch (Exception e){
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }

        return toJson;
    }

    @Override
    public ToJson<ChargeType> selectById(Integer chargeTypeId) {
        ToJson<ChargeType> toJson = new ToJson<>(1,"err");
        try{
            ChargeType chargeType = chargeTypeMapper.selectByPrimaryKey(chargeTypeId);
            if(chargeType != null){
                toJson.setFlag(0);
                toJson.setMsg("true");
                toJson.setObject(chargeType);
            }
        }catch (Exception e){
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }

        return toJson;
    }

    @Override
    public ToJson<ChargeType> selectByParent(Integer chargeTypeParent) {
        ToJson<ChargeType> toJson = new ToJson<>(1,"err");
        try{
            ChargeType chargeType = chargeTypeMapper.selectByPrimaryKey(chargeTypeParent);
            if(chargeType != null){
                List<ChargeType> chargeTypes = chargeTypeMapper.selectByParent(chargeTypeParent);
                chargeType.setChildList(chargeTypes);
                toJson.setFlag(0);
                toJson.setMsg("true");
                toJson.setObject(chargeType);
            }
        }catch (Exception e){
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }

        return toJson;
    }

    @Override
    public ToJson<ChargeType> selectChargeType(boolean type) {
        ToJson<ChargeType> toJson = new ToJson<>(1,"err");
        try{
            ChargeTypeExample chargeTypeExample = new ChargeTypeExample();
            chargeTypeExample.createCriteria().andChargeTypeParentEqualTo(0);
            List<ChargeType> chargeTypes = chargeTypeMapper.selectByExample(chargeTypeExample);
            if (chargeTypes != null && type) {
                chargeTypes.stream().forEach(chargeType -> {
                    List<ChargeType> chargeTypeList = chargeTypeMapper.selectByParent(chargeType.getChargeTypeId());
                    chargeType.setChildList(chargeTypeList);
                });
            }
            if (chargeTypes != null) {
                toJson.setFlag(0);
                toJson.setMsg("true");
                toJson.setObj(chargeTypes);
            }
        }catch (Exception e){
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }

        return toJson;
    }
}
